//
//  WeaponComposeLayer.cpp
//  FamousWarrior
//
//  Created by shuaixitao on 13-12-9.
//  Copyright (c) 2013年 Moqikaka. All rights reserved.
//

#include "WeaponComposeLayer.h"


#define TAG_SLIDER_IETM 200

// 坐标点从中心位置开始，顺时针方向布局
static CCPoint FRAGMENT_POS[5] =
{
	CCPointMake(320, 530),	// 中心顶部
	CCPointMake(520, 360),	// 右上
	CCPointMake(500, 167),	// 右下
	CCPointMake(140, 167),	// 左下
	CCPointMake(120, 360)	// 左上
};

CCScene* WeaponComposeLayer::scene()
{
	CCScene* scene = CCScene::create();
    WeaponComposeLayer* layer = WeaponComposeLayer::create();
    scene->addChild(layer);
    return scene;
}


WeaponComposeLayer::WeaponComposeLayer()
: NodeNameLog(__FILE__)
, DetailLayer("text_title_071.png", "c_108.png", true)
{
	mCurSliderIdx = 0;
}

WeaponComposeLayer::~WeaponComposeLayer()
{
	
}

bool WeaponComposeLayer::init()
{
	bool ret = DetailLayer::init();
	if (ret)
	{
		setUi();
		return true;
	}
	return false;
}

void WeaponComposeLayer::onEnter()
{
	DetailLayer::onEnter();
}

void WeaponComposeLayer::setUi()
{
	CCSprite* pSprite = NULL;
	
	// 创建背景底板
	CCScale9Sprite* background = CCScale9Sprite::create("c_003.png");
    background->setAnchorPoint(ccp(0.5, 1));
    background->setPosition(DS_BN_POS(320, 822));
    background->setContentSize(DS_BN_SIZE(630, 720));
    //addChild(background);
	
	pSprite = CCSprite::create("c_053.png");
	if (pSprite)
	{
		pSprite->setScale(MIN_SCALE);
		pSprite->setPosition(DS_BN_POS(600, 780));
		addChild(pSprite);
	}
	
	pSprite = CCSprite::create("c_053.png");
	if (pSprite)
	{
		pSprite->setScale(MIN_SCALE);
		pSprite->setPosition(DS_BN_POS(40, 780));
		pSprite->setRotation(180);
		addChild(pSprite);
	}
	
	CCSprite* pBkgSprite = CCSprite::create("sb_002.png");
	if (pBkgSprite)
	{
		pBkgSprite->setScale(MIN_SCALE);
		pBkgSprite->setPosition(DS_BN_POS(320, 395));
		addChild(pBkgSprite);
	}
	
	// 设置碎片布局UI
	/* ---------------------------------------------------------------*/
	for (int i = 0; i < 5; i++)
	{
		CCPoint pos = FRAGMENT_POS[i];
		CCTouchButton* fragment = CCTouchButton::create("sb_015.png"); //CCSprite::create("sb_015.png");
		if (fragment)
		{
			fragment->setPosition(pos);
			pBkgSprite->addChild(fragment);
		}
		CCSprite* bkgNum = CCSprite::create("CN2_GradeBg.png");
		if (bkgNum)
		{
			bkgNum->setPosition(CCPointMake(pos.x, pos.y - 68));
			pBkgSprite->addChild(bkgNum);
		}
		CCLabelTTF* lblNum = CCLabelTTF::create("0/2", FONT_NAME, 20);
		lblNum->setPosition(CCPointMake(pos.x, pos.y - 68));
		lblNum->setColor(ccYELLOW);
		pBkgSprite->addChild(lblNum);
	}
	
	Props aProp(1, 1002, 1);
	mComposeCard = CardNode::create(kCN2_Props);
	mComposeCard->setProps(aProp);

	mComposeCard->setScale(mComposeCard->getScale()/ MIN_SCALE);
	mComposeCard->setPosition(ccp(320, 350));
	//	mComposeCard->setUserData(item);
	mComposeCard->setSwallowTouch(true);
	//	mComposeCard->setTouchCallback(this, SEL_CallFuncMoveNode(&UnionYizuPreviewLayer::onViewPropsInfo));
	pBkgSprite->addChild(mComposeCard);
	
	//
	CCSprite* bkgWeaponName = CCSprite::create("sb_003.png");
	bkgWeaponName->setPosition(ccp(320, 250));
	pBkgSprite->addChild(bkgWeaponName);
	
	CCLabelTTF* mLabelWeaponName = CCLabelTTF::create("轩辕剑", FONT_NAME, 24);
	mLabelWeaponName->setPosition(ccp(320, 250));
	pBkgSprite->addChild(mLabelWeaponName);
	
	// 合成按钮
	CCTouchButton* btnCompose = CCTouchButton::create("c_008.png", "", "text_btn_025.png", PLIST_TEXT_BUTTON);
    btnCompose->setPosition(ccp(320, 42));
    btnCompose->setTargetWithActionForEvent(this, SEL_TouchHandler(&WeaponComposeLayer::onBtnComposeClicked), kTouchUpInside);
    pBkgSprite->addChild(btnCompose);
	
	const float extra_item_width = DS_SIZE(2.0f);
    mSliderItemSize = CardNode::getNodeOriginalSize(kCN2_Props);
    mSliderItemSize.width += extra_item_width;
    CCSize selFrameSize = CCTextureCache::sharedTextureCache()->addImage("zr_004.png")->getContentSizeInPixels();
	
	// 幻灯条
    const int visible_item_count = 4;
    mSliderView = CCSliderView::create(this, MIN_SIZE(mSliderItemSize.width * visible_item_count, mSliderItemSize.height), 1.0f, false);
	mSliderView->setPosition(DS_BN_POS(52, 694));
    addChild(mSliderView);
    //mSliderView->setMiddleIndex(mCurSliderIdx);
    mSliderView->reloadData();
	
	// 选中框
    mSelFrame = CCSprite::create("zr_004.png");
    mSelFrame->setAnchorPoint(ccp(0.5, 0));
    float x_sel_frame = mSliderView->getPositionX() + mSliderView->getContentSize().width / 2;
    float y_sel_frame = mSliderView->getPositionY();
    mSelFrame->setPosition(ccp(x_sel_frame, y_sel_frame));
    mSelFrame->setScale(MIN_SCALE);
    addChild(mSelFrame);
}

CCSize WeaponComposeLayer::middleItemSizeForSlider(CCSliderView *slider)
{
	return MIN_SIZE(mSliderItemSize.width, mSliderItemSize.height);
}

void WeaponComposeLayer::sliderItemAtIndex(CCSliderView *slider, CCSliderViewItem* item, int index)
{
	if (item != NULL)
    {
        // 底框
        CCSprite* bgSprite = CCSprite::create("zr_011.png");
        bgSprite->setPosition(ccp(MIN_SCALE * (mSliderItemSize.width / 2),  MIN_SCALE * (mSliderItemSize.height / 2)));
        bgSprite->setScale(MIN_SCALE);
        item->addChild(bgSprite);
		
        // 兵书卡牌
        CardNode* cardNode = CardNode::create(kCN2_Props);
        cardNode->setTag(TAG_SLIDER_IETM + index);
        cardNode->setDragEnable(false);
        cardNode->setSwallowTouch(false);
        cardNode->setZoomOnTouch(false);
        cardNode->setPosition(ccp(MIN_SCALE * (mSliderItemSize.width / 2),  MIN_SCALE * (mSliderItemSize.height / 2)));
        cardNode->setTouchCallback(this, callfuncMN_selector(WeaponComposeLayer::onSliderItemPressed));
//        int playerGrade = NetworkResData::instance()->getAvatar().grade;
//        int openHeroSlot = Formula::heroSlotOpen(playerGrade);
//        if (index < openHeroSlot)
//        {
//            const Formation& form = NetworkResData::instance()->getFormation();
//            if (index < form.size())
//            {
//                const Hero& hero = NetworkResData::instance()->getFormation()[index].hero;
//                cardNode->setHero(hero);
//            }
//            else
//            {
//                cardNode->setVisible(false);
//            }
//        }
//        else
//        {
//            cardNode->displayLocker();
//        }
        item->addChild(cardNode);
    }

}
unsigned int WeaponComposeLayer::itemCountOfSlider(CCSliderView *slider)
{
	return 4;
}

void WeaponComposeLayer::middleItemChanged(CCSliderView *slider, int newIndex)
{
 
    mCurSliderIdx = newIndex;
	refreshFragment();
}

void WeaponComposeLayer::sliderDidScrollBegin(CCSliderView *slider)
{
	mEnableSliderTouch = false;
    
//    hideCurHeroContextMenu();
     
    if (mSelFrame)
    {
        mSelFrame->setVisible(false);
    }
}
void WeaponComposeLayer::sliderDidScrollEnd(CCSliderView *slider)
{
	mEnableSliderTouch = true;
    
    if (mSelFrame)
    {
        mSelFrame->setVisible(true);
    }
}

void WeaponComposeLayer::onSliderItemPressed(CCMoveNode* node)
{
    //hideCurHeroContextMenu();
    
    if (!mEnableSliderTouch)
        return;
    
    int index = node->getTag() - TAG_SLIDER_IETM;
    if (index < mCurSliderIdx - 1 || index > mCurSliderIdx + 1)
    {
        return;
    }
    mSliderView->scroll(mCurSliderIdx - index);
}

bool WeaponComposeLayer::checkComposeCondition()
{
	// TODO: 合成条件各种判断
	return true;
}

void WeaponComposeLayer::onBtnComposeClicked(CCTouchButton* pServer, CCTouchEvent evt)
{
	
	bool key = false;
	key = checkComposeCondition();
	if (key)
	{
		// 进行合成
	}
}

void WeaponComposeLayer::refreshFragment()
{
	
}

